from django.http import *
from django.shortcuts import render
from django.utils.deprecation import MiddlewareMixin

from user.models import SysLog


class LoginMiddleWare(MiddlewareMixin):
    white_list = ['/','/user/login','/login',"login","user/login","user/user/login" ]  # 白名单
    # balck_list = ['/black/', ]  # 黑名单

    def process_request(self, request:HttpRequest):
        from django.shortcuts import redirect, HttpResponse

        next_url = request.path_info
        url=next_url[:-1]
        print(request.path_info, request.get_full_path())
        if  request.session.get('ban_urls') and request.session.get('per_urls'):
            if url in request.session['ban_urls']:
                return render(request,"404.html",locals())
            elif url in request.session['per_urls']:
                SysLog.objects.create(operate_user=request.session['user'],content=request.get_full_path())
        if next_url in self.white_list or request.session.get('user'):
            return
        else:
            return redirect("/user/login")